1
|
|
|
import { Injectable } from "@angular/core"; |
2
|
|
|
import { |
3
|
|
|
ActivatedRoute, |
4
|
|
|
ActivatedRouteSnapshot, |
5
|
|
|
CanActivate, |
6
|
|
|
CanActivateChild, |
7
|
|
|
Params, |
8
|
|
|
Router, |
9
|
|
|
RouterStateSnapshot, |
10
|
|
|
} from "@angular/router"; |
11
|
|
|
import { Observable } from "rxjs"; |
12
|
|
|
import { UniFirebaseLoginConfig } from "../config/uni-firebase-login-config"; |
13
|
|
|
import { UniFirebaseLoginConfigProvider } from "../config/uni-firebase-login-config-provider"; |
14
|
|
|
import { UserModel } from "../model/user-model"; |
15
|
|
|
import { BaseAuthService } from "../services/base-auth-service"; |
16
|
|
|
import { IGuard } from "./i-guard"; |
17
|
|
|
|
18
|
|
|
@Injectable({ |
19
|
|
|
providedIn: "root", |
20
|
|
|
}) |
21
|
|
|
export class NonAuthGuard<User extends UserModel = UserModel> |
22
|
|
|
implements CanActivate, CanActivateChild, IGuard<User> { |
23
|
|
|
protected config: UniFirebaseLoginConfig; |
24
|
|
|
|
25
|
|
|
public constructor( |
26
|
|
|
protected auth: BaseAuthService<User>, |
27
|
|
|
protected route: ActivatedRoute, |
28
|
|
|
protected router: Router, |
29
|
|
|
configProvider: UniFirebaseLoginConfigProvider, |
30
|
|
|
) { |
31
|
|
|
this.config = configProvider.config; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public canActivate( |
35
|
|
|
route: ActivatedRouteSnapshot, |
36
|
|
|
state: RouterStateSnapshot, |
37
|
|
|
): Observable<boolean> { |
38
|
|
|
return new Observable(subscriber => { |
39
|
|
|
this.auth.userInitialized$.subscribe(initialized => { |
40
|
|
|
if (initialized) { |
41
|
|
|
this.auth.user$.subscribe(user => { |
42
|
|
|
const isLoggedIn = !!user; |
43
|
|
|
if (isLoggedIn) { |
44
|
|
|
this.handleAccessDeniedRedirect(route, state).then(() => { |
45
|
|
|
subscriber.next(false); |
46
|
|
|
subscriber.complete(); |
47
|
|
|
}); |
48
|
|
|
} else { |
49
|
|
|
subscriber.next(true); |
50
|
|
|
subscriber.complete(); |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
}); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public canActivateChild( |
59
|
|
|
next: ActivatedRouteSnapshot, |
60
|
|
|
state: RouterStateSnapshot, |
61
|
|
|
): Observable<boolean> { |
62
|
|
|
return this.canActivate(next, state); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public async handleAccessDeniedRedirect( |
66
|
|
|
route: ActivatedRouteSnapshot, |
67
|
|
|
state: RouterStateSnapshot, |
68
|
|
|
): Promise<void> { |
69
|
|
|
console.log(`Access denied (NonAuthGuard): ${state.url}`); |
70
|
|
|
|
71
|
|
|
let queryParams: Params | undefined; |
72
|
|
|
if (this.config.redirectBack) { |
73
|
|
|
queryParams = this.route.snapshot.queryParams; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (queryParams && queryParams.redirectBack) { |
77
|
|
|
console.log(`Redirect back: ${queryParams.redirectBack}`); |
78
|
|
|
await this.router.navigate([queryParams.redirectBack]); |
79
|
|
|
} else if (this.config.afterSignInPage) { |
80
|
|
|
console.log(`Redirect: ${this.config.afterSignInPage}`); |
81
|
|
|
await this.router.navigate([this.config.afterSignInPage]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|